package com.itqf.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * projectName: mybatisgp8
 *
 * @author: 赵伟风
 * time: 2021/7/29 9:13
 * description:
 */
public class MybatisQueryUser {

    public static void main(String[] args) throws IOException {

        //1.sqlsessionfactoryBuilder
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //2.sqlsessionfactory

        String path = "mybatis_config.xml";

        InputStream resourceAsStream = Resources.getResourceAsStream(path);

        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
        //3.sqlsession

        SqlSession sqlSession = sqlSessionFactory.openSession();
        //4.执行
        /**
         * selectOne只能使用id查询！
         *    查询结果不能为null和大于1
         *    unique + not null   =  primary key
         *
         * ibatis
         *    每次都要找statement的唯一标识！ 还是一个字符串！ 非常容易出错！
         * 如何优化？
         */
        Object ret = sqlSession.selectOne("da.queryUserById", 1);
        System.out.println("ret = " + ret);

        //5.事务提交
        //6.资源关闭
        sqlSession.close();



    }


}
